import data from '@/api/data'

const state = {
  all: []
}

const mutations = {
  setAll(state, data) {
    state.all = data
  },

  setProductInventory(state, id) {
    // 根据id找到需要减少库存的那个产品
    const product = state.all.find(p => p.id === id)
    // 库存减一
    product.inventory--
  }
}

const actions = {
  async loadData({commit}) {
    const result = await data()
    commit('setAll', result)
  },

  // 定义使某个产品的库存减一的action
  decrementInventory({commit}, id) {
    commit('setProductInventory', id)
  }
}

const getters = {
  
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}